home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: start array at k, not 0
- Date: Thu, 04 Jan 96 20:31:26 GMT
- Organization: none
- Message-ID: <820787486snz@genesis.demon.co.uk>
- References: <Pine.OSF.3.91.960104095358.22268B-100000@io.UWinnipeg.ca>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <Pine.OSF.3.91.960104095358.22268B-100000@io.UWinnipeg.ca>
- wsimpson@uwinnipeg.ca "Bill Simpson" writes:
-
- >I have come up with the following 2 methods that allow one to talk about
- >an array that starts at element k rather than 0. E.g. 10 element array
- >y[k] to y[k+10]. Both seem to work. Is this illusory? Is one of the
- >2 ways better (or a way I haven't mentioned)?
- >
- >These programs set up y[5] to y[14].
- >
- >Thanks very much for any comments.
- >
- >Bill Simpson
- >
- >/*method 1*/
- >#include <stdio.h>
- >
- >int main(void)
- > {
- > int i, x[10];
- > int* y;
- > int offset=5; /*ie 1st index at 5, y[5]-y[14] */
- > y=x-offset;
-
- It is illegal for a pointer to point outside the bounds of an object (except
- the special case of one past the end) hence the expression x-offset is
- illegal here. The only values of offset where this would be legal are 0
- through -10.
-
- ...
-
- >/*method 2*/
- >#include <stdio.h>
- >#include <stdlib.h>
- >
- >int main(void)
- > {
- > int i;
- > int* y;
- > int offset=5;
- > int n=10;
- > y=malloc(n*sizeof(int));
-
- Were you trying to modify y by the offset here - as it stands it simply
- points at the start of the malloc'd area. Later you access up to y[14] which
- is beyond the end of the array and hence is illegal.
-
- The rules are the same for a malloc'd object as any other - you can't can't
- ue pointer arithmetic to create a pointer beyond its bounds (except one
- past the end).
-
- Therefore when approaching a problem like this you must perform any offset
- adjustments in the indices rather than the base pointers. So you could
- use:
-
- #include <stdio.h>
-
- int main(void)
- {
- int i, x[10];
- int* y;
- int offset=5; /*ie 1st index at 5, y[5]-y[14] */
-
- printf("offset=%d\n",offset);
-
- for (i=offset;i<10+offset;i++)
- {
- y[i-offset]=i;
- printf("%d\n",y[i-offset]);
- }
- return 0;
- }
-
- Or you could look at it a different way and write:
-
- #include <stdio.h>
-
- int main(void)
- {
- int i, x[10];
- int* y;
- int offset=5; /*ie 1st index at 5, y[5]-y[14] */
-
- printf("offset=%d\n",offset);
-
- for (i=0;i<10;i++)
- {
- y[i]=i+offset;
- printf("%d\n",y[i]);
- }
- return 0;
- }
-
- which is perhaps the most common 'C' way (y isn't really needed in either
- of these). This sort of problem mostly comes up when porting from other
- languages. With some rare exceptions when you are designing a program with
- C in mind from the start the problem can be avoided (e.g. as I've done in
- the last example).
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-